<?php

namespace App\Http\Middleware;

use Dcat\Admin\Http\JsonResponse;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Illuminate\Support\Str;

class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string| JsonResponse
     */
    protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            if(Str::contains($request -> route() -> getPrefix(), 'api/')) {
                return JsonResponse::make(['code' => 200, 'message' => 'Token Not Found']);
            } else {
                return route('login');
            }
        }
    }
}
